package cn.u313.service;

import cn.u313.comm.conststring.Cycle;
import cn.u313.comm.conststring.PresentEunm;
import cn.u313.comm.eunm.Type;
import cn.u313.entry.JiLuEntry;
import cn.u313.entry.PresentEntry;
import cn.u313.entry.PresentRecordEntry;
import cn.u313.entry.UserEntry;
import cn.u313.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static cn.u313.comm.conststring.PresentEunm.*;
import static cn.u313.comm.utils.Util.getTime;

@Service
public class PresentEntryService {

    @Autowired
    PresentRepository presentRepository;
    @Autowired
    PresentRecordRepository presentRecordRepository;
    @Autowired
    UserEntryRepository userEntryRepository;
    @Autowired
    JiLuEntryRepository jiLuEntryRepository;

    public String getListStr() {
        String str = "";
        List<PresentEntry> list = presentRepository.getAll();
        if(list==null||list.size()==0)
            return "暂时没有可以兑换的东西哦！";
        for (PresentEntry presentEntry : list) {
            str+=presentEntry.toString();
        }
        return str+"发送：兑换【序号】进行兑换哦！";
    }

    public String buyPresent(String code ,String qq) {
//        if(jiLuEntryRepository.countByqqAndTypeLike(qq,Type.兑换礼品+"%")!=0)
//            return "每天只能兑换一次哦！";
//            if(!(jiLuEntryRepository.countByqqAndType(qq,Type.兑换礼品+code)<entry.getNumber())){
//                return entry.getName()+"\n每天只允许兑换【"+entry.getNumber()+"】份哦";
//            }


        PresentEntry entry = presentRepository.getbyId(code);
        if(entry==null)
            return "未查找到【"+code+"】 的礼品信息";

        // TODO: 2021/12/8  兑换周期
        if(entry.getCycle()== Cycle.单次){

            if(presentRepository.countByQqAndCode(qq,code)!=0){
                return "本周期只能兑换一次哦,一般一个月一个周期！";
            } else
            if(entry.getNumber()<=presentRepository.countByCode(code)){
                // TODO: 2021/12/9 兑换数量
                return entry.getName()+"\n本周期只允许兑换【"+entry.getNumber()+"】份哦！";
            }

        }else if(entry.getCycle()== Cycle.每天){
            if(presentRepository.countByqqAndTypeAndThisToDay(qq,code)!=0){
                return entry.getName()+"\n每天只允许兑换 1 次哦！";
            }else
            if(entry.getNumber()<=presentRepository.countByTypeAndThisToDay(code)){
                // TODO: 2021/12/9 兑换数量
                return entry.getName()+"\n每天只允许兑换【"+entry.getNumber()+"】份哦！";
            }
        }





        UserEntry userEntry =userEntryRepository.byQQ(qq);
        if(userEntry==null)
            return "未找到注册信息！";



        if(自动下单类.equals(entry.getType())){
            //
        }else if(手动下单类.equals(entry.getType())){

            if(userEntry.payMe(-entry.getPrice())){
                PresentRecordEntry recordEntry = new PresentRecordEntry();
                recordEntry.setName(entry.getName());
                recordEntry.setPrice(entry.getPrice());
                recordEntry.setQq(qq);
                recordEntry.setType(未开始);
                recordEntry.setMainId(code);
                recordEntry.setTypePresent(手动下单类);
                recordEntry.setDate(new Date());
//                entry.setNumber(entry.getNumber()-1);




                JiLuEntry jiLuEntry = new JiLuEntry();
                jiLuEntry.setN(entry.getPrice());
                jiLuEntry.setQ0n(userEntry.getM()-entry.getPrice());
                jiLuEntry.setQq0(qq);
                jiLuEntry.setTime(getTime());
                jiLuEntry.setType(Type.兑换礼品+code);
                jiLuEntry.setGroupNumber("私聊");

                presentRecordRepository.save(recordEntry);
                userEntryRepository.save(userEntry);
                jiLuEntryRepository.save(jiLuEntry);

                return "兑换成功\n" +
                        "兑换："+entry.getName()+"\n" +
                        "消耗："+entry.getPrice()+"悦币\n" +
                        "余额："+userEntry.getM();
            }

            return"兑换失败！\n悦币不足【"+entry.getPrice()+"】哦";
        }else if(盲盒.equals(entry.getType())){
            //
            int r = (int)(Math.random()*100);
            int n = 1;
            if(r>60){
                n+=entry.getPrice()+entry.getPrice()*(Math.random()*Math.random());
            }else{
                n= (int)(entry.getPrice()*(Math.random()*0.6))+5;
            }
            if(userEntry.payMe(-entry.getPrice())) {

                userEntry.payMe(n);

                PresentRecordEntry recordEntry = new PresentRecordEntry();
                recordEntry.setName(entry.getName());
                recordEntry.setPrice(entry.getPrice());
                recordEntry.setQq(qq);
                recordEntry.setType(未开始);
                recordEntry.setMainId(code);
                recordEntry.setTypePresent(自动下单类);
                recordEntry.setDate(new Date());


                JiLuEntry jiLuEntry = new JiLuEntry();
                jiLuEntry.setN(entry.getPrice());
                jiLuEntry.setQ0n(userEntry.getM() - entry.getPrice());
                jiLuEntry.setQq0(qq);
                jiLuEntry.setTime(getTime());
                jiLuEntry.setType(Type.兑换礼品+code);
                jiLuEntry.setGroupNumber("私聊");

                userEntryRepository.save(userEntry);
                presentRecordRepository.save(recordEntry);
                jiLuEntryRepository.save(jiLuEntry);

                return "盲盒已开启\n" +
                        " 获得悦币：" + n + "\n" +
                        " 获得红包：0.00\n" +
                        " 你的余额：" + userEntry.getM();
            }
        }else if(大盲盒.equals(entry.getType())) {
            //
//            int r = (int) (Math.random() * 100);
//
//            int n = 1;
//            if(r>60){
//                n+=entry.getPrice()+entry.getPrice()*(Math.random()*Math.random()*Math.random());
//            }else{
//                n= (int)(entry.getPrice()*(Math.random()*0.4))+70;
//            }


            int r = (int) (Math.random() * 100);

            int n = 1;
            if(r>43){
                n+=entry.getPrice()+entry.getPrice()*(Math.random()*Math.random()*Math.random()*Math.random());
            }else{
                n= (int)(entry.getPrice()*(Math.random()*0.45))+70;
            }


            if (userEntry.payMe(-entry.getPrice())) {
                userEntry.payMe(n);
                PresentRecordEntry recordEntry = new PresentRecordEntry();
                recordEntry.setName(entry.getName());
                recordEntry.setPrice(entry.getPrice());
                recordEntry.setQq(qq);
                recordEntry.setType(未开始);
                recordEntry.setMainId(code);
                recordEntry.setTypePresent(自动下单类);
                recordEntry.setDate(new Date());


                JiLuEntry jiLuEntry = new JiLuEntry();
                jiLuEntry.setN(entry.getPrice());
                jiLuEntry.setQ0n(userEntry.getM() - entry.getPrice());
                jiLuEntry.setQq0(qq);
                jiLuEntry.setMsg(n <80?"获得0.01红包":"未获得红包");
                jiLuEntry.setTime(getTime());
                jiLuEntry.setType(Type.兑换礼品 + code);
                jiLuEntry.setGroupNumber("私聊");

//                    userEntryRepository.save(userEntry);
//                    presentRecordRepository.save(recordEntry);
//                    jiLuEntryRepository.save(jiLuEntry);

                return "测试阶段，不扣悦币\n" +
                        "盲盒已开启\n" +
                        " 获得悦币：" + n + "\n" +
                        " 获得红包："+(n <80?"0.01":"0.00")+"\n" +
                        " 你的余额：" + userEntry.getM();
            }
            return "悦币不足 ！";
        }else if(幸运大礼盒.equals(entry.getType())) {
            int r = (int) (Math.random() * 100);

            int n = 1;
            if(r>50){
                n+=entry.getPrice()+entry.getPrice()*(Math.random()*Math.random()*Math.random()*Math.random());
            }else{
                n= (int)(entry.getPrice()*(Math.random()*0.45))+(int)(entry.getPrice() * 0.7);
            }


            if (userEntry.payMe(-entry.getPrice())) {
                userEntry.payMe(n);
                PresentRecordEntry recordEntry = new PresentRecordEntry();
                recordEntry.setName(entry.getName());
                recordEntry.setPrice(entry.getPrice());
                recordEntry.setQq(qq);
                recordEntry.setType(未开始);
                recordEntry.setMainId(code);
                recordEntry.setTypePresent(自动下单类);
                recordEntry.setDate(new Date());


                JiLuEntry jiLuEntry = new JiLuEntry();
                jiLuEntry.setN(entry.getPrice());
                jiLuEntry.setQ0n(userEntry.getM() - entry.getPrice());
                jiLuEntry.setQq0(qq);
                jiLuEntry.setMsg(n <80?"获得0.01红包":"未获得红包");
                jiLuEntry.setTime(getTime());
                jiLuEntry.setType(Type.兑换礼品 + code);
                jiLuEntry.setGroupNumber("私聊");

                    userEntryRepository.save(userEntry);
                    presentRecordRepository.save(recordEntry);
                    jiLuEntryRepository.save(jiLuEntry);

                return "幸运礼盒已开启\n" +
                        " 获得悦币：" + n + "\n" +
                        " 获得红包："+(n <80?"0.01":"0.00")+"\n" +
                        " 你的余额：" + userEntry.getM();
            }
            return "悦币不足 ！";
        }

        return "没有匹配奖励类型！";



    }



    public String waitPresent() {
        String str ="";
        List<PresentRecordEntry> list = presentRecordRepository.getAll(未开始);
        if(list==null||list.size()==0){
            return "暂时没有待处理的单子哦";
        }
        for (PresentRecordEntry recordEntry : list) {
            str+=recordEntry.toString();
        }
        return str;
    }

    public String waitPresent(String code, String qq,String msg) {
        PresentRecordEntry presentRecordEntry = presentRecordRepository.getById(未开始,code);
        if(presentRecordEntry==null)
            return"没有找到订单哦!可能你已经处理了！";

        if(msg.contains("退")){
            presentRecordEntry.setType(已退款);
            presentRecordRepository.save(presentRecordEntry);
            return "用户："+presentRecordEntry.getQq()+"\n" +
                    "商品："+presentRecordEntry.getName()+"\n" +
                    "已退款："+presentRecordEntry.getPrice()+"悦币";
        }else{
            presentRecordEntry.setType(已开始);
            presentRecordRepository.save(presentRecordEntry);
            return "用户："+presentRecordEntry.getQq()+"\n" +
                    "奖励："+presentRecordEntry.getName()+"\n" +
                    "已发放";
        }

    }
}
